package api

import (
	"admin100/app/model"
	"admin100/app/service"
	"admin100/library/define"
	"admin100/library/define/defErr"
	"admin100/library/response"

	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
)

var MailSmtpDataMgrApi = MailSmtpDataMgr{}

type MailSmtpDataMgr struct {
}

// @summary 后台工具-邮件SMTP设置-新增
// @tags 后台工具
// @produce json
// @param   User query string true "SMTP账号"
// @param   Host query string true "SMTP HOST"
// @param   Port query int true "SMTP PORT"
// @param   Pass query string true "密码"
// @param   State query int true "开关状态"
// @router  /mail-smtp-data/add [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *MailSmtpDataMgr) Add(r *ghttp.Request) {
	var (
		req   model.MailSmtpDataMgrApiAddReq
		sData model.MailSmtpDataMgrServicesAddReq
	)

	if err := r.ParseForm(&req); err != nil {
		response.JsonExitWithMsg(r, define.Fail, err.Error())
	}

	err := gconv.Struct(req, &sData)
	if err != nil {
		defErr.Panic(define.ReqParamsIllegal)
	}

	res := service.MailSmtpDataService.Add(&sData)

	response.JsonSuc(r, res)

}

// @summary 后台工具-邮件SMTP设置-修改
// @tags 后台工具
// @produce json
// @param   User query string false "SMTP账号"
// @param   Host query string false "SMTP HOST"
// @param   Port query int false "SMTP PORT"
// @param   Pass query string false "密码"
// @param   State query int false "开关状态"
// @router  /mail-smtp-data/edit [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *MailSmtpDataMgr) Edit(r *ghttp.Request) {
	var (
		req   model.MailSmtpDataMgrApiEditReq
		sData model.MailSmtpDataMgrServicesEditReq
	)

	if err := r.ParseForm(&req); err != nil {
		response.JsonExitWithMsg(r, define.Fail, err.Error())
	}

	err := gconv.Struct(req, &sData)
	if err != nil {
		defErr.Panic(define.ReqParamsIllegal)
	}

	res := service.MailSmtpDataService.Edit(&sData)

	response.JsonSuc(r, res)

}

// @summary 后台工具-邮件SMTP设置-修改
// @tags 后台工具
// @produce json
// @param   users query int true "SMTP账号"
// @router  /mail-smtp-data/del [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *MailSmtpDataMgr) Del(r *ghttp.Request) {
	var (
		req   model.MailSmtpDataMgrApiDelReq
		sData model.MailSmtpDataMgrServicesDelReq
	)

	if err := r.ParseForm(&req); err != nil {
		response.JsonExitWithMsg(r, define.Fail, err.Error())
	}

	err := gconv.Struct(req, &sData)
	if err != nil {
		defErr.Panic(define.ReqParamsIllegal)
	}

	res := service.MailSmtpDataService.Del(&sData)

	response.JsonSuc(r, res)

}

// @summary 后台工具-邮件SMTP设置-修改
// @tags 后台工具
// @produce json
// @param   page query int true "当前页数"
// @param   pageSize query int true "每页数据数量"
// @router  /mail-smtp-data/list [GET]
// @success 200 {object} response.JsonResponse "执行结果" {object} common.PageInfo "分页信息"
func (a *MailSmtpDataMgr) List(r *ghttp.Request) {
	var (
		req   model.MailSmtpDataMgrApiListReq
		sData model.MailSmtpDataMgrServiceListReq
	)

	if err := r.ParseForm(&req); err != nil {
		response.JsonExitWithMsg(r, define.Fail, err.Error())
	}

	err := gconv.Struct(req, &sData)
	if err != nil {
		defErr.Panic(define.ReqParamsIllegal)
	}

	pageInfo, data := service.MailSmtpDataService.List(&sData)

	response.JsonSuc(r, map[string]interface{}{
		"total": pageInfo,
		"rows":  data,
	})
}
